package com.demo.connectdbtest.module.export.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.demo.connectdbtest.module.export.domain.PromotorExcel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/excel")
public class ExportExcelController {


    @RequestMapping("/export")
    public void batchInsert(MultipartFile file) throws IOException {
        List<PromotorExcel> users = new ArrayList<>();
        List<PromotorExcel> userExcels = new ArrayList<>();

        // 读excel
        EasyExcel.read(file.getInputStream(), PromotorExcel.class, new AnalysisEventListener<PromotorExcel>() {
            @Override
            public void invoke(PromotorExcel userExcel, AnalysisContext analysisContext) {
               // User user = new User();
              //  BeanUtils.copyProperties(userExcel, user);
                //users.add(user);
                userExcels.add(userExcel);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                // 存储到数据库中
                // users.forEach(user -> baseMapper.insert(user));
                System.out.println("excel文件读取完毕！");
                userExcels.forEach(System.out::println);
            }
        }).sheet().doRead();
        ;

        // 写excel
        EasyExcel.write(new File("D://输入用户信息.xlsx"), PromotorExcel.class).sheet().doWrite(userExcels);
    }

}
